/**
 *  *2010-5-3 下午04:22:55  add 
 */
package com.base.myproject.client.busi.business;

import com.base.myproject.client.jdbc.DataSet;
import com.base.myproject.client.system.Env;
import com.base.myproject.client.tools.CommandSyncContainer;
import com.base.myproject.client.tools.CommandSyncRatify;
import com.base.myproject.client.tools.CommandSyncReturnObject;
import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.Style.VerticalAlignment;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.Dialog;
import com.extjs.gxt.ui.client.widget.Info;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.VerticalPanel;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.TextArea;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.layout.TableData;
import com.extjs.gxt.ui.client.widget.layout.TableLayout;
import com.extjs.gxt.ui.client.widget.toolbar.ToolBar;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;

/**
 * 审批对话框，审批人通过对话框操作审批
 * 
 * @author Administrator 2010-5-3 javazgw@gmail.com
 */
public class BusinessRatifyDiaolog {

	
	RatifyInfo ratifyinfo = new RatifyInfo();
//	String busi, icode;
	TextField<String> danju = new TextField<String>();
	TextField<String> bcode = new TextField<String>();
	TextArea ratifydesc = new TextArea();
	com.extjs.gxt.ui.client.widget.FramePanel frame_flow = com.extjs.gxt.ui.client.widget.FramePanel
			.pop();
	com.extjs.gxt.ui.client.widget.FramePanel frame_ratifyed = com.extjs.gxt.ui.client.widget.FramePanel
			.pop();
	com.extjs.gxt.ui.client.widget.FramePanel frame_detail = com.extjs.gxt.ui.client.widget.FramePanel
	.pop();

	FormPanel formpanel = new FormPanel();
	ToolBar tb = new ToolBar();
	Button agree = new Button("同意");
	Button disagree = new Button("不同意");
	Button detail = new Button("查看明细");
	ContentPanel detailcp = new ContentPanel();
	VerticalPanel cp = new VerticalPanel();
	ContentPanel topcp = new ContentPanel();
	Dialog d = new Dialog();
	public BusinessRatifyDiaolog(RatifyInfo ri ) {
//		this.busi = busi;
//		this.icode = icode;
		this.ratifyinfo = ri;
		init();
	}

	void init() {
		initForm();
		initButtonBar();
		initWidget();
		show();
	}

	void initWidget() {
		frame_ratifyed.setBorders(false);
		frame_flow.setBorders(false);

		
		
		d.setSize(778, 499);
		
		detailcp.setCollapsible(true);
		detailcp.setHeaderVisible(false);
		detailcp.collapse();
		topcp.setCollapsible(true);
		topcp.setHeaderVisible(false);
		
		TableLayout tl = new TableLayout(2);
		TableData td1 = new TableData();
		td1.setColspan(1);
		td1.setRowspan(1);
		td1.setVerticalAlign(VerticalAlignment.TOP);
		td1.setHeight("310px");
		td1.setWidth("370px");
		TableData td2 = new TableData();
		td2.setColspan(2);
		td2.setRowspan(1);

		TableData td3 = new TableData();
		td3.setColspan(2);
		td3.setRowspan(1);

		topcp.setLayout(tl);

		topcp.add(frame_flow, td1);
		topcp.add(formpanel, td1);
		cp.add(topcp);
		
		
		
		detailcp.add(frame_detail);
		cp.add(detailcp);
//		cp.add(tb);
		d.setBottomComponent(tb);
		d.add(cp);
	

	}

	void show()
	{
		d.show();
		load();
	}
	void initForm() {
		formpanel.setHeaderVisible(false);
		danju.setFieldLabel("单据");
		bcode.setFieldLabel("提交人");
		ratifydesc.setFieldLabel("审批意见");
		ratifydesc.setHeight("100px");
//		danju.setWidth(285);
//		bcode.setWidth(285);
//		ratifydesc.setWidth(285);
		formpanel.setFieldWidth(250);
		formpanel.add(danju);
		formpanel.add(bcode);
		formpanel.add(frame_ratifyed);
		formpanel.add(ratifydesc);

	}

	boolean isfirst=true;
	void initButtonBar() {
		tb.setAlignment(HorizontalAlignment.CENTER);
		tb.add(agree);
		tb.add(disagree);
		tb.add(detail);

		disagree.addListener(Events.Select, getListener());
		agree.addListener(Events.Select, getListener());
		detail.addListener(Events.Select, getListener());
	}

	void load() {
		
		com.base.myproject.client.tools.GreetingServiceAsync greetingService = GWT
				.create(com.base.myproject.client.tools.GreetingService.class);
		greetingService.getDataSet("select 'aa' as danju ,'我' as bcode ", getAsyncCallback());

	}

	private AsyncCallback<DataSet> getAsyncCallback() {

		AsyncCallback<DataSet> callback = new AsyncCallback<DataSet>() {

			@Override
			public void onFailure(Throwable caught) {

			}

			@Override
			public void onSuccess(DataSet result) {
				if(result.getRowCount()>0)
				{
				danju.setValue(result.getValue(0,"danju"));
				bcode.setValue(result.getValue(0, "bcode"));
				frame_flow.el().setElementAttribute("src", "./busijsp/ratify/getratifyimage.jsp?busicode="+ratifyinfo.getBusi());
				frame_flow.el().setElementAttribute("height", "378px");
				frame_flow.el().setElementAttribute("width", "370px");
				
				frame_ratifyed.el().setElementAttribute("src", "./busijsp/ratify/ratifyed.jsp?busicode="+ratifyinfo.getBusi()+"&innercode="+ratifyinfo.getIcode());
				frame_ratifyed.el().setElementAttribute("height", "172px");
				frame_ratifyed.el().setElementAttribute("width", "370px");
				
				
				}
				else
				{
					
				}
			}

		};
		return callback;
	}
	
	
	int sync = 0;                                                      
	Listener<BaseEvent> defaultlistener;                               
                                                                     
	Listener<BaseEvent> getListener() {                                
		if(defaultlistener!=null)                                        
		{                                                                
			return defaultlistener;                                        
		}                                                                
                                                                     
		defaultlistener = new Listener<BaseEvent>() {                    
			public void handleEvent(BaseEvent be) {                        
				sync++;                                                      
				try {                                                        
					if (sync == 1) {                                           
						if (be.getType() == Events.Select) {                     
							if (be.getSource().equals( agree)) {                            
                                           
								CommandSyncContainer list = new CommandSyncContainer();
								
								CommandSyncRatify ratify = new CommandSyncRatify();
								
								list.add(ratify);
								
								
								ratifyinfo.setType(1);
								ratifyinfo.setDescription(ratifydesc.getValue());
								ratifyinfo.getVariables().put("content", "to task2");
								ratify.setRatifyinfo(ratifyinfo);
								
								com.base.myproject.client.tools.GreetingServiceAsync greetingService = GWT
								.create(com.base.myproject.client.tools.GreetingService.class);
						greetingService.SendCommandSync(list, new AsyncCallback<CommandSyncReturnObject> ()
								{

									@Override
									public void onFailure(Throwable caught) {
										
										MessageBox.alert("错误","网络连接错误，请重新尝试", null);
									}

									@Override
									public void onSuccess(
											CommandSyncReturnObject result) {
										if(result.isB())
										{
											Info.display("", "审批成功");
										}
										else
										{
											MessageBox.alert("错误", result.getMessage(), null);
										}
									}
							
								});
								
							}    
							if (be.getSource().equals( disagree)) {                            
	                            
                                
								CommandSyncContainer list = new CommandSyncContainer();
								
								CommandSyncRatify ratify = new CommandSyncRatify();
								
								list.add(ratify);
								
								
								ratifyinfo.setType(2);
								ratifyinfo.setDescription(ratifydesc.getValue());
								ratifyinfo.getVariables().put("content", "to task2");
								ratify.setRatifyinfo(ratifyinfo);
								
								com.base.myproject.client.tools.GreetingServiceAsync greetingService = GWT
								.create(com.base.myproject.client.tools.GreetingService.class);
						greetingService.SendCommandSync(list, new AsyncCallback<CommandSyncReturnObject> ()
								{

									@Override
									public void onFailure(Throwable caught) {
										
										MessageBox.alert("错误","网络连接错误，请重新尝试", null);
									}

									@Override
									public void onSuccess(
											CommandSyncReturnObject result) {
										if(result.isB())
										{
											Info.display("", "审批成功");
										}
										else
										{
											MessageBox.alert("错误", result.getMessage(), null);
										}
									}
							
								});
								
							
							} 
							if (be.getSource().equals( detail)) {    
								
								if(detailcp.isCollapsed())
								{
									topcp.collapse();
									detailcp.expand();
									detail.setText("审批信息");
									if(isfirst)
									{
								frame_detail.el().setElementAttribute("src", "./busijsp/ratify/ratifydetail.jsp?busicode="+ratifyinfo.getBusi()+"&innercode="+ratifyinfo.getIcode());
									
									frame_detail.el().setElementAttribute("height", "378px");
									frame_detail.el().setElementAttribute("width", "755px");
									isfirst= false;
									}
								}
								else
								{
									topcp.expand();
									detailcp.collapse();
									detail.setText("查看明细");
								}
								
                                
							} 
						} else if (be.getType() == Events.KeyUp) {               
							if (be instanceof ComponentEvent) {                    
								if (((ComponentEvent) be).getKeyCode() == 13) // 回车
								{                                                    
								}                                                    
							}                                                      
						}                                                        
					}                                                          
				} finally {                                                  
					sync--;                                                    
				}                                                            
			}                                                              
                                                                     
		};                                                               
		return defaultlistener;                                          
	}

	public RatifyInfo getRatifyinfo() {
		return ratifyinfo;
	}

	public void setRatifyinfo(RatifyInfo ratifyinfo) {
		this.ratifyinfo = ratifyinfo;
	} 
	
	
}
